Page({
  data: {
    id: '',
    houseInfo: '',
    validTime: '',
    encryptedData: '',
    url: '',
    type: '',
  },

  onLoad({ id, encryptedData }) {
    id && this.getPassPort(id)
    encryptedData && this.getSharePassPort(encryptedData)
  },
  async getSharePassPort(encryptedData) {
    console.log(encryptedData)
    const {
      code,
      data: { id, houseInfo, url, validTime },
    } = await wx.http.get(`/visitor/share/${encryptedData}`)
    if (code === 10000) {
      this.setData({
        id,
        houseInfo,
        url,
        validTime,
        type: 'share',
      })
    }
  },
  async getPassPort(id) {
    const {
      code,
      data: { id: ID, houseInfo, validTime, encryptedData, url },
    } = await wx.http.get(`/visitor/${id}`)
    if (code === 10000) {
      this.setData({
        id: ID,
        houseInfo,
        validTime,
        encryptedData,
        url,
      })
    }
  },
  async savePhoto() {
    await wx.getImageInfo({
      src: this.data.url,
      success: function ({ path }) {
        // success
        console.log(path, '成功')
        wx.saveImageToPhotosAlbum({
          filePath: path,
          success: function () {
            wx.utils.toast('通行证已保存至相册系统!')
          },
          fail: function () {
            console.log('保存失败！')
          },
        })
      },
      fail: function () {
        // fail
        console.log('失败')
      },
      complete: function () {
        // complete
      },
    })
  },
  onShareAppMessage() {
    const { encryptedData } = this.data
    console.log(encryptedData)
    return {
      title: '查看通行证',
      path: `/visitor_pkg/pages/passport/index?encryptedData=${encryptedData}`,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
})
